package com.juqimiao.raccoon.commodity.service.impl;

import com.juqimiao.linn.command.CommandBusI;
import com.juqimiao.linn.context.BusinessPointContext;
import com.juqimiao.linn.context.ContextManager;
import com.juqimiao.linn.context.BusinessPoint;
import com.juqimiao.raccoon.commodity.appobj.CategoryGroupListAO;
import com.juqimiao.raccoon.commodity.dto.AddCategoryGroupCmd;
import com.juqimiao.raccoon.commodity.dto.SortCatgoryGroupCmd;
import com.juqimiao.raccoon.commodity.dto.UpdateCategoryGroupCmd;
import com.juqimiao.raccoon.commodity.dto.query.ListCategoryGroupQry;
import com.juqimiao.linn.transfer.Response;
import com.juqimiao.linn.transfer.query.QueryResponse;
import com.juqimiao.raccoon.commodity.dto.SwitchCategoryGroupPublishCmd;
import com.juqimiao.raccoon.commodity.service.CategoryGroupServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class CategoryGroupService implements CategoryGroupServiceI {

    @Autowired
    private CommandBusI commandBus;

    @Override
    public Response createGroup(String uerId, AddCategoryGroupCmd cmd) {
       // TenantContext context = new TenantContext(uerId, getTenantId(uerId), getBusinessPoint());
        BusinessPointContext context = new BusinessPointContext();
        context.setCurrent(getBusinessPoint());
        ContextManager.setContext(context);
        return commandBus.send(cmd);
    }

    @Override
    public Response updateGroup(String userId, UpdateCategoryGroupCmd cmd){
        BusinessPointContext context = new BusinessPointContext();
        context.setCurrent(getBusinessPoint());
        ContextManager.setContext(context);
        return commandBus.send(cmd);
    }

    @Override
    public QueryResponse<List<CategoryGroupListAO>> list(String userId, ListCategoryGroupQry qry) {
        //TenantContext context = new TenantContext(userId, getTenantId(userId), getBusinessPoint());
        BusinessPointContext context = new BusinessPointContext();
        context.setCurrent(getBusinessPoint());
        ContextManager.setContext(context);
        return (QueryResponse<List<CategoryGroupListAO>>) commandBus.send(qry);
    }

    @Override
    public Response publishGroup(String userId, SwitchCategoryGroupPublishCmd cmd) {
       // TenantContext context = new TenantContext(userId, getTenantId(userId), getBusinessPoint());
        BusinessPointContext context = new BusinessPointContext();
        context.setCurrent(getBusinessPoint());
        ContextManager.setContext(context);
        return commandBus.send(cmd);
    }

    @Override
    public Response sort(SortCatgoryGroupCmd cmd) {
        BusinessPointContext context = new BusinessPointContext();
        context.setCurrent(getBusinessPoint());
        ContextManager.setContext(context);
        return commandBus.send(cmd);
    }

    private String getTenantId(String userId) {
        return userId + "998";
    }

    private BusinessPoint getBusinessPoint() {
        /* BusinessPoint bp = BusinessPoint.valueOf("center-platform", "user-case-a", "scenario-a"); */
        return BusinessPoint.valueOf("center-platform");
    }
}
